<?php

namespace app\admin\controller;

use app\models\{Userlist};
use think\Db;
use think\Validate;

class Superv extends Base
{

    public function index()
    {
        $data = $this->request->get();
        $results = (new Userlist())->order(['id' => 'desc']);
        if (!empty($data['title'])) {
            $results = $results->where('account|name', 'like', '%' . $data['title'] . '%');
        }
        if (!empty($data['type'])) {
            $results = $results->where('type', $data['type']);
        }
        $results = $results->where('del', 1);
        $typearr = config('config.typepeople');
        $type = config('config.type');
        $results = $results->paginate(10, false, ['query' => $this->request->param()]);
        foreach($results as $result){
            $result['project_text'] = '';
            $result['type_text'] = $typearr[$result['type']] ?? '无';
            if($result['project'] == 0){
                $result['project_text'] = '全部项目';
            }else{
                $projects = explode(',',$result['project']) ?? [];
                foreach ($projects as $project){
                    if(!empty($type[$project])){
                        $result['project_text'] .= '['.$type[$project].']';
                    }
                }
            }
        }
        return view(null, [
            'list' => $results
        ]);
    }
    
public function user_edit()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $validate = new Validate([
                'account' => 'require',
                'name' => 'require',
                'type' => 'require',
                'ptype' =>  'require',
            ], [
                'account.require' => '账号不能为空',
                'name.require' => '姓名不能为空',
                'type.require' => '类型不能为空',
                'ptype.require' => '是否全部项目不能为空',
            ]);
            if (!$validate->check($data)) {
                return json(['status' => 1, 'msg' =>  $validate->getError()]);
            }
            if (!empty($data['password'])) {
                $data['password'] = md5(md5($data['password']));
            }
            if (isset($data['password']) && empty($data['password'])) {
                unset($data['password']);
            }
            if($data['ptype'] == 1){
                $data['project'] = 0;
            }else{
                if(count($data['project'] ?? []) <= 0){
                    return json(['status' => 1, 'msg' => '请选择项目']);
                }
                $data['project'] = implode(',',$data['project'] ?? []);
            }
            if (!empty($data['id'])) {
                $res = (new Userlist())->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $user = (new Userlist())->where(['account' => $data['account'],'del' =>1])->find();
                if(!empty($user)){
                    return json(['status' => 1, 'msg' => '账号已存在']);
                }
                if (empty($data['password'])) {
                    return json(['status' => 1, 'msg' => '密码不能为空']);
                }
                $res = (new Userlist())->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['status' => 0, 'msg' => '编辑成功']);
            }
            return json(['status' => 1, 'msg' => '编辑失败']);
        } else {
            $id = $this->request->get('id/d', 0);
            $row = (new Userlist())->where('id', $id)->find();
            if($row){
                if($row['project'] != 0){
                    $row['project'] = explode(',',$row['project']) ?? [];
                }
            }
            return view('', [
                'row' => $row
            ]);
        }
    }

    public function user_del()
    {
        if ($this->request->isPost()) {
            $id = $this->request->post('id/d', 0);
            if (empty($id)) {
                return json(['status' => 1, 'msg' => '请选择删除项目']);
            }
            $user = (new Userlist())->where('id', $id)->find();
            if (empty($user)) {
                return json(['status' => 1, 'msg' => '用户不存在，请重试']);
            }
            $res = (new Userlist())->where('id', $id)->update(['del' => 2]);
            if ($res) {
                return json(['status' => 0, 'msg' => '操作成功']);
            }
            return json(['status' => 1, 'msg' => '操作失败']);
        }
    }
    
}